package org.redis.cluster.scheduler.minute;

import java.util.concurrent.atomic.AtomicLong;

import org.redis.cluster.RedisCluster;
import org.redis.cluster.event.Event;
import org.redis.cluster.event.EventListener;
import org.redis.cluster.plugin.Plugin;
import org.redis.cluster.scheduler.second.SecondScheduleEvent;

public class MinuteScheduler implements EventListener<SecondScheduleEvent>,Plugin {
	
	private final AtomicLong minute=new AtomicLong();
	
	private RedisCluster redisCluster;
	
	@Override
	public void init(RedisCluster redisCluster) {
		this.redisCluster=redisCluster;
	}
	
	@Override
	public void destroy() {
	}
	
	@Override
	public void onEvent(SecondScheduleEvent event) {
		if(!(event.getSecond()>=60 && event.getSecond()%60==0)) {
			return;
		}
		
		redisCluster.getEventPusher().push(new MinuteScheduleEvent(minute.incrementAndGet()));
	}

	@Override
	public boolean isSupport(Event event) {
		return (event instanceof SecondScheduleEvent);
	}
}